package com.unisedu.hr.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.unisedu.hr.basic.IBaseEntity;
import com.unisedu.hr.basic.IBaseService;
import com.unisedu.hr.basic.user.BasicPersonInfo;
import com.unisedu.hr.basic.user.EmployeeInfo;
import com.unisedu.hr.dao.EmployeeDao;
import com.unisedu.hr.entity.Employee;
import com.unisedu.hr.layui.Table;
import com.unisedu.hr.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional(readOnly = true)
public class EmployeeService implements IBaseService{

    private EmployeeDao employeeDao;


    @Autowired
    public EmployeeService (EmployeeDao employeeDao){
        this.employeeDao = employeeDao;
    }

    @Override
    public IBaseEntity get(Integer id) {
        return employeeDao.get(id);
    }

    public Employee getEmployeeInfo(Integer id){
        return employeeDao.getEmployeeInfo(id);
    }

    @Override
    public List<IBaseEntity> findList(IBaseEntity entity) {
        return employeeDao.findList(entity);
    }

    @Override
    public Table findPage(IBaseEntity entity, Integer page, Integer limit) {
        PageHelper.startPage(page,limit);
        List<IBaseEntity> list = findList(entity);
        PageInfo<IBaseEntity> pageInfo = new PageInfo<>(list);
        return new Table(pageInfo);
    }

    @Override
    @Transactional
    public IBaseEntity insert(IBaseEntity entity) {
        BasicPersonInfo personInfo = ((Employee) entity).getPersonInfo();
        EmployeeInfo employeeInfo = ((Employee) entity).getEmployeeInfo();
        employeeDao.insertPersonInfo(personInfo);
        employeeInfo.setPersonId(personInfo.getId());
        if(StringUtil.isEmpty(employeeInfo.getTestEndDate())){
            employeeInfo.setTestEndDate(null);
        }
        employeeDao.insertEmployeeInfo(employeeInfo);
        return entity;
    }

    @Override
    @Transactional
    public IBaseEntity update(IBaseEntity entity) {
        BasicPersonInfo personInfo = ((Employee) entity).getPersonInfo();
        EmployeeInfo employeeInfo = ((Employee) entity).getEmployeeInfo();
        employeeDao.updatePersonInfo(personInfo);
        employeeDao.updateEmployeeInfo(employeeInfo);
        return entity;
    }

    @Transactional
    public void updateEmployeePhoto(EmployeeInfo info){
        employeeDao.updateEmployeePhoto(info);
    }


    @Override
    public Integer delete(Integer id) {
        return null;
    }
}
